var app = getApp();
var listOffset = 0;
var goodstype, sort, sort_type;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    listhgt: 0,
    goods_width: 100,
    goods_pic_width: 25,
    goods_pic_rest_width: 75,
    hasMore: true,
    goodslist: [],
    blankshow: false,
    blanktips: '~没有收藏记录~',
    collectLoaded:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    goodstype = options.goodstype;
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        var tmphgt = res.windowHeight;
        that.setData({
          listhgt: tmphgt
        });
      },
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    app.showLoading();
    listOffset = 0;
    this.loadList(listOffset);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return app.shareToIndex();
  },
  loadMoreGoods: function () {
    listOffset++;
    this.loadList(listOffset);
  },
  goodsTap: function (event) {
    var goodsid = event.currentTarget.dataset.goodsid;
    wx.navigateTo({
      url: '../../store/goodsdetail/goodsdetail?goodsid=' + goodsid,
    })
  },
  loadList: function (offset) {
    var url = '/index.php/api/User/collect_list';
    var params = {
      page: offset
    }
    app.request(url, params).then(this.callBackFuc.bind(this));
  },
  callBackFuc: function (result) {
    app.hideLoading();
    var dataArr = result.result.goods_list;
    if (dataArr.length <= 0) {
      var blankshow = false;
      if (listOffset == 0) {
        blankshow = true;
      }
      this.setData({
        blankshow: blankshow
      });
    }
    var orginal_arr = [];
    if (listOffset > 0) {
      orginal_arr = this.data["goodslist"];
    }
    for (var i = 0; i < dataArr.length; i++) {
      var goodsinfo = {};
      goodsinfo.collectID = dataArr[i].collect_id;
      goodsinfo.goodsID = dataArr[i].goods_id;
      goodsinfo.storecount = dataArr[i].store_count;
      goodsinfo.oprice = dataArr[i].shop_price;
      goodsinfo.pic = app.globalData.webUrl + dataArr[i].original_img;

      goodsinfo.name = dataArr[i].goods_name;
      var namestr = goodsinfo.name;
      var long_name = namestr, short_name = namestr;
      if (namestr.length > 15) {
        short_name = namestr.substr(0, 15) + '...';
      }
      if (namestr.length > 30) {
        long_name = namestr.substr(0, 30) + '...';
      }
      goodsinfo.shortname = short_name;
      goodsinfo.longname = long_name;
      orginal_arr.push(goodsinfo);
    }
    this.setData({
      goodslist: orginal_arr,
      collectLoaded: true
    });
  },
  cancelCollect: function (event) {
    // app.showLoading();
    var catid = event.currentTarget.dataset.catid;
    var url = '/index.php/api/User/cancel_collect';
    var params = {
      collect_id: catid
    };
    app.request(url, params).then(this.cancelCollectCallBack.bind(this));
  },
  cancelCollectCallBack: function (res) {
    // app.hideLoading();
    var that = this;
    wx.showToast({
      duration:1000,
      icon: 'none',
      title: res.msg
    });
    var delayid = setTimeout(function(){
      clearTimeout(delayid);
      app.showLoading();
      that.loadList(listOffset);
    },1000);
  }
})